package com.sskj.xyft.main;

import android.accounts.NetworkErrorException;

import com.google.gson.JsonSyntaxException;
import com.hjq.toast.ToastUtils;
import com.sskj.base.api.OtherLoginEvent;
import com.sskj.base.api.OtherLoginException;
import com.sskj.xyft.BasePresenter;
import com.sskj.base.data.User;
import com.sskj.xyft.api.ApiObserver;
import com.sskj.base.utils.RxUtils;
import com.sskj.xyft.api.Api;
import com.sskj.xyft.mine.data.AppVersionBean;

import org.greenrobot.eventbus.EventBus;

import java.net.SocketTimeoutException;

public class MainPresenter extends BasePresenter<MainActivity> {

    public void checkVersion(String versionName) {
        Api.getInstance().checkVersion(1, versionName)
                .compose(RxUtils.transform())
                .subscribe(new ApiObserver<AppVersionBean>(this) {
                    @Override
                    public void onSuccess(AppVersionBean data) {
                        mView.checkVersion(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        onComplete();
                        if (e instanceof NetworkErrorException) {
                            e.printStackTrace();
                        } else if (e instanceof JsonSyntaxException) {
                            e.printStackTrace();
                        } else if (e instanceof SocketTimeoutException) {
                            ToastUtils.show("网络连接超时，请重试");
                        } else if (e instanceof OtherLoginException) {
                            EventBus.getDefault().post(new OtherLoginEvent());
                        } else {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void getAccountDetail(String account, boolean showLoading) {
        Api.getInstance().accountDetail()
                .compose(RxUtils.transform())
                .subscribe(new ApiObserver<User>(this, showLoading) {
                    @Override
                    public void onSuccess(User data) {
                        mView.setAccountInfo(data);
                    }
                });
    }


}
